import { getarticlelabel, getarticlelist, getcategorylist, getknowledgeDetail, getknowledgelist, } from '@/services';
import { ArticleLabel, knowledgeDetail, knowledgeObject, tuiObject, wenObject } from '@/types';
import { Effect, Reducer, Subscription } from 'umi';

export interface knowledgeModelState {
    knowledgegorylist: knowledgeObject[],
    knowledgegorynum: number,
    tuilist: tuiObject[],
    categorylist: wenObject[],
    knowledgeDetail: knowledgeDetail,
    ArticleLabel:ArticleLabel[],
}
export interface knowledgeModelType {
    namespace: 'knowledge';

    state: knowledgeModelState;//ArticleLabel
    effects: {
        knowledge: Effect;
        articlelist: Effect;
        categorylist: Effect;
        knowledgeDetail: Effect;
        getarticlelabel: Effect;
    };
    reducers: {
        save: Reducer<knowledgeModelState>;
    };
}

const knowledgeModel: knowledgeModelType = {
    namespace: 'knowledge',

    state: {
        knowledgegorylist: [],
        knowledgegorynum: 0,
        tuilist: [],
        categorylist: [],
        knowledgeDetail: {} as knowledgeDetail,
        ArticleLabel:[]
    },

    effects: {
        //知识小册接口
        *knowledge({ payload }, { call, put }) {
            const result = yield call(getknowledgelist);
            if (result.data) {
                yield put({
                    type: "save",
                    payload: {
                        knowledgegorylist: result.data[0],
                        knowledgegorynum: result.data[1]
                    }
                })
            }
        },
        //文章阅读接口
        *articlelist({ payload }, { call, put }) {
            const result = yield call(getarticlelist);
            if (result.data) {
                yield put({
                    type: "save",
                    payload: {
                        tuilist: result.data,
                    }
                })
            }
        },
        //推荐阅读接口
        *categorylist({ payload }, { call, put }) {
            const result = yield call(getcategorylist);
            if (result.data) {
                yield put({
                    type: "save",
                    payload: {
                        categorylist: result.data,
                    }
                })
            }
        },
        //文章标签接口
        *getarticlelabel({ payload }, { call, put }) {
            const result = yield call(getarticlelabel);
            if (result.data) {
                yield put({
                    type: "save",
                    payload: {
                        ArticleLabel: result.data,
                    }
                })
            }
        },
        //详情接口
        * knowledgeDetail({ payload }, { call, put }) {
            const result = yield call(getknowledgeDetail, payload);
            if (result.data) {
                yield put({
                    type: "save",
                    payload: {
                        knowledgeDetail: result.data,
                    }
                })
            }
        }
    },

    reducers: {
        save(state, action) {
            return {
                ...state,
                ...action.payload,
            };
        },
    },
};

export default knowledgeModel;